<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>品牌数据</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>


<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script src="js/axios-0.18.0.js"></script>


<!--DIV-->
<div id="app">



    <el-form :inline="true" :model="Brand" class="demo-form-inline">
        <el-form-item label="当前状态">
            <el-select v-model="Brand.status" placeholder="不填不给你查">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="企业名称">
            <el-input v-model="Brand.company_name" placeholder="你最好给我填"></el-input>
        </el-form-item>
        <el-form-item label="品牌名称">
            <el-input v-model="Brand.brand_name" placeholder="写上吧孩子"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>

    <!--新增和删除按钮-->
    <el-row>
        <el-button type="primary" @click="dialogVisible = true">新增</el-button>
        <el-button type="danger" @click="deleteByids">批量删除</el-button>
    </el-row>

    <!--添加数据的对话框-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="dialogVisible"
            width="30%"
            >
        <el-form ref="form" :model="Brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="Brand.brand_name"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="Brand.company_name"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="Brand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="Brand.description"></el-input>
            </el-form-item>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="Brand.status" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addBrand">立即创建</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"><!--复选框选中以后会选中这一整行的数据(方法）-->
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    prop="brand_name"
                    label="品牌名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="company_name"
                    label="企业名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    label="排序"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    label="状态"
                    align="center">
            </el-table-column>
            <el-table-column
                    label="操作"
                    align="center">
                <el-row>
                    <el-button type="primary">修改</el-button>
                    <el-button type="danger">删除</el-button>
                </el-row>
            </el-table-column>
        </el-table>
    </template>

    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>

<!--VUE-->
<script>


    new Vue({
        el:"#app",
        mounted(){

            //this.selectAll();
            /*var _this = this;

            axios({
                method:"get",
                url:" http://localhost:8080/mystudy2/brand/selectAll"
            }).then(function (resp) {
                _this.tableData=resp.data;
            })*/

            /*var _this = this;

            axios({
                method:"post",
                url:" http://localhost:8080/mystudy2/brand/selectByPageAndCondition?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
                data:this.Brand
            }).then(function (resp) {
                _this.totalCount=resp.data.totalCount;
                _this.tableData=resp.data.rows;
            })*/
            this.selectAll();


        },
        methods: {

            //批量删除
            deleteByids(){

                for (let i = 0; i < this.multipleSelection.length; i++) {
                    let selectionElement = this.multipleSelection[i];
                    this.selectedids[i]=selectionElement.ID;
                }

                var _this = this;
                axios({
                    method:"post",
                    url:"http://localhost:8080/mystudy2/brand/deleteByids",
                    data:_this.selectedids
                }).then(function (resp) {
                    if (resp.data=="success"){

                        _this.selectAll();

                        _this.$message({
                            message: '恭喜你，删除成功',
                            type: 'success'
                        });
                    }
                })

            },
            selectBycondition(){
                var _this = this;
                axios({
                    method:"post",
                    url:"http://localhost:8080/mystudy2/brand/selectByPageAndCondition?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
                    data:this.Brand
                }).then(function (resp) {
                    _this.totalCount=resp.data.totalCount;
                    _this.tableData=resp.data.rows;
                })

            },


            //查询所有
            selectAll(){
                var _this = this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/mystudy2/brand/selectByPage?currentPage="+_this.currentPage+"&pageSize="+_this.pageSize,
                    data:{}
                }).then(function (resp) {
                    _this.totalCount=resp.data.totalCount;
                    _this.tableData=resp.data.rows;
                })
            },

            //添加表单数据
            addBrand(){
                var _this = this;

                axios({
                    method:"post",
                    url: "http://localhost:8080/mystudy2/brand/add",
                    data:_this.Brand
                }).then(function (resp) {
                    if (resp.data=="success"){
                        _this.tableData=resp.data
                        _this.selectAll();
                        _this.dialogVisible = false;
                        _this.$message({
                                message: '恭喜你，添加成功!',
                                type: 'success'
                            });
                    }
                })

            },
            onSubmit() {
                //console.log('submit!');
                this.selectBycondition();
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            //复选框选中以后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;//复选框中所有数据的集合
            },
            handleSizeChange(val) {
                //console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.selectAll();

            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.selectAll();
            }
        },
        data() {
            return {

                //初始总记录数
                totalCount:100,

                //被选中的ID会被加入到这个数组当中
                selectedids:[],

                //页码
                currentPage: 1,

                //显示每页条数
                pageSize:5,

                //对话框表单数据
                form: {
                    name: '',
                    region: '',
                    date1: '',
                    date2: '',
                    delivery: false,
                    type: [],
                    resource: '',
                    desc: ''
                },

                //添加对话框的展示标记
                dialogVisible: false,
                //搜索表单
                Brand: {
                    status: '',
                    brand_name: '',
                    company_name: '',
                    description:'',
                    ordered:'',
                    ID:''
                },
                //复选框选中返回的数据
                multipleSelection: [],

                tableData: [{
                    brand_name: '华为',
                    company_name: '华为公司',
                    ordered: '深圳',
                    status:'1'
                }, {
                    brand_name: '华为',
                    company_name: '华为公司',
                    ordered: '深圳',
                    status:'1'
                }, {
                    brand_name: '华为',
                    company_name: '华为公司',
                    ordered: '深圳',
                    status:'1'
                }, {
                    brand_name: '华为',
                    company_name: '华为公司',
                    ordered: '深圳',
                    status:'1'
                }]
            }
        }




    })
</script>




</body>
</html>